﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_169_MajorityElement
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int s = MajorityElement(new int[] {6,6,6,7,7});
            Console.WriteLine(s);

            Console.ReadKey();
        }
        public static int MajorityElement(int[] nums)
        {
            if (nums.Length==1) return nums[0];
              
            int num = nums.Length / 2;
            int res = 0;
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < nums.Length; i++)
            {
                if (dic.ContainsKey(nums[i]))
                {
                    dic[nums[i]]++;
                    if (dic[nums[i]]>= num)
                    {
                        res = nums[i];
                    }
                }
                else
                {
                    dic.Add(nums[i],1);
                }
            }
            return res;
        }
    }
}
